{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2 Python 语言介绍与概览"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 上机练习：体验Python程序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 程序1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Enter the datetime(20200812): 20200813\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "226\n"
     ]
    }
   ],
   "source": [
    "# 输入某年某月末日，判断这一天是这一年的第几天？\n",
    "import datetime\n",
    "\n",
    "dtstr = input(\"Enter the datetime(20200812):\")\n",
    "dt = datetime.datetime.strptime(dtstr, \"%Y%m%d\")\n",
    "another_dtstr = dtstr[:4] + '0101'\n",
    "anoter_dt = datetime.datetime.strptime(another_dtstr, \"%Y%m%d\")\n",
    "print(int((dt - anoter_dt).days) + 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 程序2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "input a string: I love you 666!\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "There are 8 lettes, 3 spaces, 3 digits and 1 other characters in your string.\n"
     ]
    }
   ],
   "source": [
    "# 输入一行字符，分别统计出其中英文字母、空格、数字和其他字符的个数\n",
    "import string\n",
    "\n",
    "s = input(\"input a string:\")\n",
    "letter = 0\n",
    "space = 0 \n",
    "digit = 0\n",
    "other = 0\n",
    "for c in s:\n",
    "    if c.isalpha():\n",
    "        letter += 1\n",
    "    elif c.isspace():\n",
    "        space += 1\n",
    "    elif c.isdigit():\n",
    "        digit += 1\n",
    "    else:\n",
    "        other += 1\n",
    "print(\"There are {:d} lettes, {:d} spaces, {:d} digits and {:d} other characters in your string.\".\n",
    "      format(letter, space, digit, other))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 程序3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 8, 8, 13, 16, 16, 18, 20, 22, 24, 31, 38, 39, 39, 39, 40, 40, 41, 43, 47, 47, 48, 49, 51, 51, 51, 57, 60, 63, 66, 66, 67, 67, 69, 72, 72, 77, 81, 81, 82, 85, 87, 89, 90, 92, 93, 94, 97, 99, 99]\n"
     ]
    }
   ],
   "source": [
    "# merge sort\n",
    "# 归并排序\n",
    "import random\n",
    "\n",
    "\n",
    "def merge_sort(data_list):\n",
    "    if len(data_list) <= 1:\n",
    "        return data_list\n",
    "    middle = int(len(data_list) / 2)\n",
    "    left = merge_sort(data_list[:middle])\n",
    "    right = merge_sort(data_list[middle:])\n",
    "    merged = []\n",
    "    while left and right:\n",
    "        merged.append(left.pop(0) if left[0] <= right[0] else right.pop(0))\n",
    "    merged.extend(right if right else left)\n",
    "    return merged\n",
    "\n",
    "\n",
    "data_list = [random.randint(1, 100) for _ in range(50)]  # 列表生成式\n",
    "print(merge_sort(data_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 程序4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "猜数游戏！我想了一个1-100的数字，你最多可以猜6次，看看能猜出来吗？\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "1-100的整数，第1次猜，请输入： 50\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不好意思，你的数大了一点儿！\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "1-100的整数，第2次猜，请输入： 25\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不好意思，你的数大了一点儿！\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "1-100的整数，第3次猜，请输入： 12\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不好意思，你的数大了一点儿！\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "1-100的整数，第4次猜，请输入： 6\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不好意思，你的数小了一点儿！\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "1-100的整数，第5次猜，请输入： 9\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不好意思，你的数小了一点儿！\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "1-100的整数，第6次猜，请输入： 10\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不好意思，你的数小了一点儿！\n",
      "哎呀！怎么也没猜中！再见！\n"
     ]
    }
   ],
   "source": [
    "# -*- coding:utf-8 -*-\n",
    "# 猜数字游戏\n",
    "import random\n",
    "\n",
    "secret = random.randint(1, 100)\n",
    "print(\"猜数游戏！我想了一个1-100的数字，你最多可以猜6次，看看能猜出来吗？\")\n",
    "tries = 1\n",
    "while tries <= 6:\n",
    "    guess = int(input(\"1-100的整数，第{:d}次猜，请输入：\".format(tries)))\n",
    "    if guess == secret:\n",
    "        print(\"恭喜答对了！你只猜了{:d}次！就是这个：{:d}\".format(tries, secret))\n",
    "        break\n",
    "    elif guess > secret:\n",
    "        print(\"不好意思，你的数大了一点儿！\")\n",
    "    else:\n",
    "        print(\"不好意思，你的数小了一点儿！\")\n",
    "    tries += 1\n",
    "else:\n",
    "    print(\"哎呀！怎么也没猜中！再见！\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 程序5 (Thonny or IDLE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      " 1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0 -30.0\n"
     ]
    }
   ],
   "source": [
    "x = float(input())\n",
    "a = 2\n",
    "b = -45\n",
    "c = 13\n",
    "y = a * x ** 2 + b * x + c\n",
    "print(x, y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 程序6 (Thonny or IDLE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      " 5\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    @\n",
      "   @@@\n",
      "  @@@@@\n",
      " @@@@@@@\n",
      "@@@@@@@@@\n"
     ]
    }
   ],
   "source": [
    "n = int(input())\n",
    "for i in range(n):\n",
    "    line = \" \" * (n - 1 - i) + \"@\" * (2 * i + 1)\n",
    "    print(line)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc-autonumbering": false,
  "toc-showcode": false,
  "toc-showmarkdowntxt": false
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
